"""Base cleaner abstract class for data cleaning pipeline"""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
import logging

logger = logging.getLogger(__name__)


class BaseCleaner(ABC):
    """Abstract base class for all data cleaners
    
    Defines the interface that all cleaning components must implement
    """
    
    def __init__(self, config: Optional[Dict[str, Any]] = None):
        """Initialize the cleaner with optional configuration
        
        Args:
            config: Optional configuration dictionary for the cleaner
        """
        self.config = config or {}
        self.enabled = self.config.get('enabled', True)
        self.setup()
    
    def setup(self):
        """Setup method for initialization logic
        
        Override this method in subclasses for custom initialization
        """
        pass
    
    @abstractmethod
    def clean(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Clean the input data
        
        Args:
            data: Input data dictionary to be cleaned
            
        Returns:
            Cleaned data dictionary
            
        Note:
            This method must be implemented by all subclasses
        """
        pass
    
    def process(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Process the data through this cleaner
        
        Args:
            data: Input data dictionary
            
        Returns:
            Processed data dictionary (cleaned if enabled)
        """
        if not self.enabled:
            logger.debug(f"{self.__class__.__name__} is disabled, skipping")
            return data
        
        try:
            logger.debug(f"Processing data through {self.__class__.__name__}")
            return self.clean(data)
        except Exception as e:
            logger.error(f"Error in {self.__class__.__name__}: {e}")
            # Return original data on error to maintain pipeline flow
            return data
    
    def validate_input(self, data: Dict[str, Any]) -> bool:
        """Validate input data before processing
        
        Args:
            data: Input data to validate
            
        Returns:
            True if data is valid, False otherwise
        """
        if not isinstance(data, dict):
            logger.error(f"Invalid input type: expected dict, got {type(data)}")
            return False
        return True
    
    @property
    def name(self) -> str:
        """Get the name of this cleaner
        
        Returns:
            Name of the cleaner class
        """
        return self.__class__.__name__
    
    def get_stats(self) -> Dict[str, Any]:
        """Get statistics about this cleaner's operations
        
        Returns:
            Dictionary containing statistics
        """
        return {
            'name': self.name,
            'enabled': self.enabled,
            'config': self.config
        }